package com.tencent.sr.iris.basic.saas.dependency;

import com.alibaba.fastjson.JSON;
import com.tencent.sr.iris.basic.saas.common.utils.HttpUtils;
import com.tencent.sr.iris.basic.service.interfaces.client.CosClient;
import com.tencent.sr.iris.basic.service.interfaces.client.NoticeClient;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticeBaseRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticePageRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.NoticeSaveRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.UploadRichTextRequest;
import com.tencent.sr.iris.basic.service.interfaces.response.NoticeDetailResponse;
import com.tencent.sr.iris.basic.service.interfaces.response.NoticePageResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class NoticeDependency {

    @Resource
    private NoticeClient noticeClient;
    
    @Resource
    private CosClient cosClient;

    @Resource
    private AppUser appUser;

    public PaginationResponse<NoticePageResponse> page(PaginationRequest<NoticePageRequest> request) {
        log.info("NoticeDependency.page调用，入参：{}", JSON.toJSONString(request));
        HttpResult<PaginationResponse<NoticePageResponse>> result = noticeClient.page(request);
        return HttpUtils.handleResult(result);
    }


    public Boolean save(NoticeSaveRequest request) {
        request.setUid(appUser.getUid());
        log.info("NoticeDependency.save调用，入参：{}", JSON.toJSONString(request));

        UploadRichTextRequest richTextRequest = new UploadRichTextRequest();
        richTextRequest.setRichText(request.getContent());
        HttpResult<String> richText = cosClient.uploadRichText(richTextRequest);
        String url = HttpUtils.handleResult(richText);
        request.setContent(url);

        HttpResult<Boolean> result = noticeClient.save(request);
        return HttpUtils.handleResult(result);
    }

    public NoticeDetailResponse detail(NoticeBaseRequest request) {
        log.info("NoticeDependency.detail调用，入参：{}", JSON.toJSONString(request));
        HttpResult<NoticeDetailResponse> result = noticeClient.detail(request);
        return HttpUtils.handleResult(result);
    }

    public Boolean delete(NoticeBaseRequest request) {
        request.setUid(appUser.getUid());
        log.info("NoticeDependency.delete调用，入参：{}", JSON.toJSONString(request));
        HttpResult<Boolean> result = noticeClient.delete(request);
        return HttpUtils.handleResult(result);
    }

    public Boolean publish(NoticeBaseRequest request) {
        request.setUid(appUser.getUid());
        log.info("NoticeDependency.publish调用，入参：{}", JSON.toJSONString(request));
        HttpResult<Boolean> result = noticeClient.publish(request);
        return HttpUtils.handleResult(result);
    }

}
